#include <iostream>
#include <vector>
#include <fstream>
#include <time.h>
#include <chrono>

std::vector<int> bubble(std::vector<int> w) {
    int temp;
    bool swapped = false;
    for(int i = w.size(); i > 0; i--) {
        bool swapped = false;
        for (int j = 0; j < i-1; j++) {
            if (w[j] > w[j+1]) {
                std::swap(w[j], w[j+1]);
                swapped = true;
            }
        }
        if(!swapped) {
            break;
        }
    }
    return w;
}

long getTime() {
    namespace sc = std::chrono;
    auto time = sc::system_clock::now(); // get the current time
    auto since_epoch = time.time_since_epoch(); // get the duration since epoch
    auto millis = sc::duration_cast<sc::milliseconds>(since_epoch);
    long now = millis.count();
    return now;
}

void repeatCode(std::string name) {
    std::string line;
    std::fstream in(name);
    std::vector<int> a = {};
    if (in.is_open()) {
        while (std::getline(in, line))
        {
            a.push_back(std::stoi(line));
        }
    }
    long start = getTime();
    std::vector<int> result = bubble(a);
    long end = getTime();
    std::cout << name << " " << (end-start) << " milliseconds" << std::endl;
    in.close();
}

int main() {
    repeatCode("m1_sorted.txt");    
    repeatCode("m2_reverse.txt");    
    repeatCode("m3_shuffle.txt");    
}